<template>
  <div class="login-container">
    <van-nav-bar title="登录" @click-left="onClickLeft" left-arrow />
    <van-form  ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        type="number"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        maxlength="6"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-button
            v-if="falg"
            class="send-sms-btn verification"
            round
            size="small"
            type="default"
            @click="verification"
            >点击发送验证码</van-button
          >
          <van-count-down v-else :time="time" format="ss s" @finish="finish" />
        </template>
      </van-field>

      <div style="margin: 16px">
        <van-button block type="info" native-type="submit" @click="onSubmit"> 登录 </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Login, getSmsCode } from '@/api/user'

export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '',
        code: '',
      },
      time: 10 * 1000,
      falg: true,
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空',
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号码格式不正确',
          },
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空',
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码输入不正确',
          },
        ],
      },
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onClickLeft() {
      this.$router.go(-1)
    },

    // 获取验证码事件
    finish() {
      this.falg = true
    },
    async onSubmit(values) {
      // console.log(values,1111)

      this.$toast.loading({
        duration: 0.5, // 持续时间，0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '登录中...', // 提示消息
      })
      // 登录请求

      try {

        const { data : res } = await Login(this.user)

        this.$toast.success('登录成功')
        // console.log(res, '6666')
        this.$router.push('/my')
        // 要将登录后的信息存起来，呀存到vuex 里面去， 同时要将token做本地缓存
        this.$store.commit ('setUser', res.data ) 
        this.$store.commit('change')
        

      } catch (err) {
         console.log(err);
         
        if (err.response.status === 400) {
          console.log('登录失败', err)
          this.$toast.fail('登录失败，手机号或验证码错误')
        } else {
          this.$toast.fail('登录超时， 请稍后再试')
        }
      }
    },

    // 验证码请求
    async verification() {
      try {
        await this.$refs.loginForm.validate('mobile')
        this.falg = false
        const res = await getSmsCode(this.user.mobile)
        console.log(res)
      } catch (error) {
        console.log(error);
        
        this.$toast.fail('验证失败')
      }
    },
  },
}
</script>

<style scoped lang='less'>
.van-nav-bar {
  background-color: #3296fa;
  /deep/.van-nav-bar__title {
    color: #fff;
  }
  /deep/.van-icon-arrow-left {
    color: #fff;
  }
}
.van-form {
  .iconfont {
    font-size: 40px;
  }
  /deep/.van-field__control {
    font-size: 14px;
    margin-left: 10px;
  }
  /deep/.van-count-down {
    margin-right: 20px;
  }
  /deep/.verification {
    background-color: #ededed;
    font-size: 12px;
    color: #666;
  }
}
</style>